<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'团队长等级管理'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$return['list']=$this->getall('level',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
		$item['conditions']='预先团长';
	}
	//添加默认等级
	$default_levelset=$this->plugin_setting('default_level');
	$default_level=array(
		'id'=>'0',
		'title'=>$default_levelset['title']?:'预选团长',
		'level'=>0,
		'conditions'=>'预选团长',
		'fee_per'=>$default_levelset['fee_per'],
		'fee_per2'=>$default_levelset['fee_per2'],
		'red_per'=>$default_levelset['red_per'],
	);
	array_unshift($return['list'],$default_level);
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、预选团长也有奖励，可以点击设置！
		</br>2、团队长级差奖：上级团队长奖励-下级团队长奖励-下下级-下下下级，以此类推！
		','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			//array('name'=>'area_type','title'=>'适用代理类型','type'=>'radio','options'=>'area_types'),
			array('name'=>'level','title'=>'等级权重（数字越大，权重越大）','type'=>'string'),
			array('type'=>'unitstring','unit'=>'%','name'=>'red_per','title'=>'团队长级差奖（%）'),
		),'action'=>array(
			array('op'=>'look_condition','title'=>'升级条件','type'=>'modal','show'=>array('level >'=>0)),
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
			array('op'=>'set_default','title'=>'设置默认','type'=>'href','show'=>array('level'=>0)),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look_condition'){
	$item=$this->get('level',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('conditions'));
	if(!$item){
		exi('指定等级不存在！','error');
	}
	$item['conditions']=json_decode($item['conditions'],true);
	$return['item']=$item;
	if(!$item['conditions']){
		exi('该等级尚未设置升级条件！','error');
	}
	$htmls=array(
		'type'=>'look',
		'look'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$htmls['look'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array();
		$item['setdata']=$item['setdata']?json_decode($item['setdata'],true):array('commission1'=>'0.00');
	}else{
		$item=array(
			'title'=>'',
			'conditions'=>array('order_price'=>'0'),
			'setdata'=>array('commission1'=>'0.00'),
		);
	}
   
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'填写一百以内的数字，数字越大表示等级越高！不同等级的等级权重不得相等！'),
			//array('name'=>'isyidong','title'=>'是否开启团长转移','type'=>'switch','help'=>'开启后，扫本等级团长二维码后，才会变更上级'),
			//array('name'=>'set_group','title'=>'扫本等级团长二维码指定等级','type'=>'select','options'=>'groups','help'=>'客户扫了本等级的二维码后，会直接绑定为选择的等级'),
		))),
	);
	//升级条件表单
	$condition_edit=array(
		'title'=>'升级条件',
		'list'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	$conditions=array();
	foreach($current_level_up_types as $type){
		if($type=='level_num'){
			$conditions['level_id']=$item['conditions']['level_id'];
			$condition_edit['list'][]=array('type'=>'select','name'=>'conditions.level_id','title'=>'选择指定等级','options'=>'level');
		}
		$conditions[$type]=$item['conditions'][$type];
		$condition_edit['list'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$item['conditions']=$conditions;
	$htmls['edits'][]=$condition_edit;
	$commission_edit=array(
		'title'=>'团队长级差奖',
		'list'=>array(
			array('type'=>'unitstring','unit'=>'%','name'=>'red_per','title'=>'比例（%）','help'=>'如果是最低团长等级，本<red>设置无效</red>！【<red>团队长级差奖</red>】：指上级团队长拿下级团队长的下级，下下级、下下下级，此次类推，都有代理奖励。其中下级团队长的下级，下下级、下下下级，此次类推，有人和下级团队长平级就没有代理奖励！'),
		),
	);
	$htmls['edits'][]=$commission_edit;
	$return['item']=$item;
	$return['pingfee_dtype']=$this->pingfee_dtype;
	$return['daili_titype']=$return['pingfee_titype']=$this->daili_titype;
	$return['level']=$this->pdo_getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		//'area_type'=>intval($postdata['area_type']),
		'level'=>intval($postdata['level']),
		'conditions'=>$postdata['conditions']?ijson_encode($postdata['conditions']):'',
		'fee_per'=>trim($postdata['fee_per']),
		'fee_per2'=>trim($postdata['fee_per2']),
		'red_per'=>trim($postdata['red_per']),
		'isyidong'=>intval($postdata['isyidong']),
		'set_group'=>intval($postdata['set_group']),
		'jindoufee'=>trim($postdata['jindoufee']),
		'teamredfee'=>trim($postdata['teamredfee']),
		'open_maxteamfee'=>intval($postdata['open_maxteamfee']),
		'maxteamfee_rate'=>trim($postdata['maxteamfee_rate']),
		'setdata'=>ijson_encode($postdata['setdata']),
	);
	if($data['level']<=0 || $data['level']>100){
		exi('请填写正确的等级权重值，1-100','error');
	}
	
	$id=intval($postdata['id']);
	if($id){
		$islevel=$this->getcolumn('level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'level'=>$postdata['level'],'id <>'=>$id),'id');
		if($islevel){
			exi('该权重已设置过，不能重复！','error');
		}
		$this->update('level',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$islevel=$this->getcolumn('level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'level'=>$postdata['level']),'id');
		if($islevel){
			exi('该权重已设置过，不能重复！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('level',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set_default'){
	//设置默认分红比例
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('default_level',$post);
		exi('默认等级分红比例设置成功！','','list');
	}
	$item=$this->plugin_setting('default_level');
	if(!$item){
		$item=array('commission1'=>'0.00');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置默认分红比例',
		'edits'=>array(),
	);
	$commission_edit=array(
		'title'=>'预先团长重命名',
		'list'=>array(
			array('type'=>'string','name'=>'title','title'=>'重命名'),
		),
	);
	$htmls['edits'][]=$commission_edit;
	$commission_edit=array(
		'title'=>'团队长级差奖',
		'list'=>array(
			array('type'=>'unitstring','unit'=>'%','name'=>'red_per','title'=>'比例（%）','help'=>'如果是最低团长等级，本<red>设置无效</red>！【<red>团队长级差奖</red>】：指上级团队长拿下级团队长的下级，下下级、下下下级，此次类推，都有代理奖励。其中下级团队长的下级，下下级、下下下级，此次类推，有人和下级团队长平级就没有代理奖励！'),
		),
	);
	$return['daili_titype']=$this->daili_titype;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('level',array('id'=>$id));	
	}
	if(!$id){
		exi('默认等级不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('level',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>